package gsoc.ogsadai.model.diagram.navigator;

import gsoc.ogsadai.model.DataType;
import gsoc.ogsadai.model.InputType;
import gsoc.ogsadai.model.OutputStreamType;
import gsoc.ogsadai.model.diagram.edit.parts.ActivityTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.ActivityTypeInstanceNameEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.DataTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.InputStreamTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.InputStreamTypePipeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.InputTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.InputsTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.OutputStreamTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.OutputStreamTypePipeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.OutputsTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.ParallelType2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.ParallelTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.PipelineType2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.PipelineTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.SequenceType2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.SequenceTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WorkflowType2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WorkflowType3EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WorkflowTypeEditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabel2EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabel3EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabel4EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabel5EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabel6EditPart;
import gsoc.ogsadai.model.diagram.edit.parts.WrappingLabelEditPart;
import gsoc.ogsadai.model.diagram.part.RequestDiagramEditorPlugin;
import gsoc.ogsadai.model.diagram.part.RequestVisualIDRegistry;
import gsoc.ogsadai.model.diagram.providers.RequestElementTypes;
import gsoc.ogsadai.model.diagram.providers.RequestParserProvider;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserService;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class RequestNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		RequestDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put(
						"Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		RequestDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put(
						"Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof RequestNavigatorItem
				&& !isOwnView(((RequestNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof RequestNavigatorGroup) {
			RequestNavigatorGroup group = (RequestNavigatorGroup) element;
			return RequestDiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof RequestNavigatorItem) {
			RequestNavigatorItem navigatorItem = (RequestNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (RequestVisualIDRegistry.getVisualID(view)) {
		case WorkflowTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://ogsadai.org.uk/namespaces/2007/04/types?WorkflowType", RequestElementTypes.WorkflowType_79); //$NON-NLS-1$
		case PipelineTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://ogsadai.org.uk/namespaces/2007/04/types?PipelineType", RequestElementTypes.PipelineType_1001); //$NON-NLS-1$
		case ParallelTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://ogsadai.org.uk/namespaces/2007/04/types?ParallelType", RequestElementTypes.ParallelType_1002); //$NON-NLS-1$
		case SequenceTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://ogsadai.org.uk/namespaces/2007/04/types?SequenceType", RequestElementTypes.SequenceType_1003); //$NON-NLS-1$
		case ActivityTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?ActivityType", RequestElementTypes.ActivityType_2001); //$NON-NLS-1$
		case InputsTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?InputsType", RequestElementTypes.InputsType_2002); //$NON-NLS-1$
		case InputTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?InputType", RequestElementTypes.InputType_2003); //$NON-NLS-1$
		case InputStreamTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?InputStreamType", RequestElementTypes.InputStreamType_2004); //$NON-NLS-1$
		case DataTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?DataType", RequestElementTypes.DataType_2005); //$NON-NLS-1$
		case OutputsTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?OutputsType", RequestElementTypes.OutputsType_2006); //$NON-NLS-1$
		case OutputStreamTypeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?OutputStreamType", RequestElementTypes.OutputStreamType_2007); //$NON-NLS-1$
		case WorkflowType2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?WorkflowType", RequestElementTypes.WorkflowType_2008); //$NON-NLS-1$
		case PipelineType2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?PipelineType", RequestElementTypes.PipelineType_2009); //$NON-NLS-1$
		case ParallelType2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?ParallelType", RequestElementTypes.ParallelType_2010); //$NON-NLS-1$
		case SequenceType2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?SequenceType", RequestElementTypes.SequenceType_2011); //$NON-NLS-1$
		case WorkflowType3EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://ogsadai.org.uk/namespaces/2007/04/types?WorkflowType", RequestElementTypes.WorkflowType_2012); //$NON-NLS-1$
		case OutputStreamTypePipeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://ogsadai.org.uk/namespaces/2007/04/types?OutputStreamType?pipe", RequestElementTypes.OutputStreamTypePipe_3001); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = RequestDiagramEditorPlugin.getInstance()
				.getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& RequestElementTypes.isKnownElementType(elementType)) {
			image = RequestElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof RequestNavigatorGroup) {
			RequestNavigatorGroup group = (RequestNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof RequestNavigatorItem) {
			RequestNavigatorItem navigatorItem = (RequestNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (RequestVisualIDRegistry.getVisualID(view)) {
		case WorkflowTypeEditPart.VISUAL_ID:
			return getWorkflowType_79Text(view);
		case PipelineTypeEditPart.VISUAL_ID:
			return getPipelineType_1001Text(view);
		case ParallelTypeEditPart.VISUAL_ID:
			return getParallelType_1002Text(view);
		case SequenceTypeEditPart.VISUAL_ID:
			return getSequenceType_1003Text(view);
		case ActivityTypeEditPart.VISUAL_ID:
			return getActivityType_2001Text(view);
		case InputsTypeEditPart.VISUAL_ID:
			return getInputsType_2002Text(view);
		case InputTypeEditPart.VISUAL_ID:
			return getInputType_2003Text(view);
		case InputStreamTypeEditPart.VISUAL_ID:
			return getInputStreamType_2004Text(view);
		case DataTypeEditPart.VISUAL_ID:
			return getDataType_2005Text(view);
		case OutputsTypeEditPart.VISUAL_ID:
			return getOutputsType_2006Text(view);
		case OutputStreamTypeEditPart.VISUAL_ID:
			return getOutputStreamType_2007Text(view);
		case WorkflowType2EditPart.VISUAL_ID:
			return getWorkflowType_2008Text(view);
		case PipelineType2EditPart.VISUAL_ID:
			return getPipelineType_2009Text(view);
		case ParallelType2EditPart.VISUAL_ID:
			return getParallelType_2010Text(view);
		case SequenceType2EditPart.VISUAL_ID:
			return getSequenceType_2011Text(view);
		case WorkflowType3EditPart.VISUAL_ID:
			return getWorkflowType_2012Text(view);
		case OutputStreamTypePipeEditPart.VISUAL_ID:
			return getOutputStreamTypePipe_3001Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getWorkflowType_79Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getPipelineType_1001Text(View view) {

		IAdaptable hintAdapter = new RequestParserProvider.HintAdapter(
				RequestElementTypes.PipelineType_1001,
				(view.getElement() != null ? view.getElement() : view),
				RequestVisualIDRegistry
						.getType(WrappingLabelEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);
		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getParallelType_1002Text(View view) {

		IAdaptable hintAdapter = new RequestParserProvider.HintAdapter(
				RequestElementTypes.ParallelType_1002,
				(view.getElement() != null ? view.getElement() : view),
				RequestVisualIDRegistry
						.getType(WrappingLabel3EditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);
		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getSequenceType_1003Text(View view) {

		IAdaptable hintAdapter = new RequestParserProvider.HintAdapter(
				RequestElementTypes.SequenceType_1003,
				(view.getElement() != null ? view.getElement() : view),
				RequestVisualIDRegistry
						.getType(WrappingLabel4EditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);
		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getActivityType_2001Text(View view) {
		IAdaptable hintAdapter = new RequestParserProvider.HintAdapter(
				RequestElementTypes.ActivityType_2001,
				(view.getElement() != null ? view.getElement() : view),
				RequestVisualIDRegistry
						.getType(ActivityTypeInstanceNameEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			RequestDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 4002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getInputsType_2002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getInputType_2003Text(View view) {
		InputType domainModelElement = (InputType) view.getElement();
		if (domainModelElement != null) {
			return String.valueOf(domainModelElement.getName());
		} else {
			RequestDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 2003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getInputStreamType_2004Text(View view) {
		IAdaptable hintAdapter = new RequestParserProvider.HintAdapter(
				RequestElementTypes.InputStreamType_2004,
				(view.getElement() != null ? view.getElement() : view),
				RequestVisualIDRegistry
						.getType(InputStreamTypePipeEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			RequestDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 4001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getDataType_2005Text(View view) {
		DataType domainModelElement = (DataType) view.getElement();
		if (domainModelElement != null) {
			return String.valueOf(domainModelElement.getString());
		} else {
			RequestDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 2005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getOutputsType_2006Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getOutputStreamType_2007Text(View view) {
		OutputStreamType domainModelElement = (OutputStreamType) view
				.getElement();
		if (domainModelElement != null) {
			return String.valueOf(domainModelElement.getName());
		} else {
			RequestDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 2007); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getWorkflowType_2008Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getPipelineType_2009Text(View view) {

		IAdaptable hintAdapter = new RequestParserProvider.HintAdapter(
				RequestElementTypes.PipelineType_2009,
				(view.getElement() != null ? view.getElement() : view),
				RequestVisualIDRegistry
						.getType(WrappingLabel2EditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);
		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getParallelType_2010Text(View view) {

		IAdaptable hintAdapter = new RequestParserProvider.HintAdapter(
				RequestElementTypes.ParallelType_2010,
				(view.getElement() != null ? view.getElement() : view),
				RequestVisualIDRegistry
						.getType(WrappingLabel5EditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);
		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getSequenceType_2011Text(View view) {

		IAdaptable hintAdapter = new RequestParserProvider.HintAdapter(
				RequestElementTypes.SequenceType_2011,
				(view.getElement() != null ? view.getElement() : view),
				RequestVisualIDRegistry
						.getType(WrappingLabel6EditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);
		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getWorkflowType_2012Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getOutputStreamTypePipe_3001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return WorkflowTypeEditPart.MODEL_ID.equals(RequestVisualIDRegistry
				.getModelID(view));
	}

}
